//
//  UserGuideView.m
//  etionUI
//
//  Created by WangJian on 14-2-24.
//  Copyright (c) 2014年 GuangZhouXuanWu. All rights reserved.
//

#import "UserGuideView.h"
#import "UserGuideBackgroundView.h"
#import "UserGuideCircleNoteView.h"

@implementation CUserGuideUnit

+ (id)unitWithType:(EUserGuideUnitType)type area:(CGRect)area text:(NSString *)text image:(UIImage *)image
{
    CUserGuideUnit *unit = [[[CUserGuideUnit alloc] init] autorelease];
    unit.type = type;
    unit.area = area;
    unit.text = text;
    unit.image = image;
    unit.textFont = [UIFont boldSystemFontOfSize:16];
    return unit;
}

- (void)dealloc
{
    [_text release_mm];
    [_image release_mm];
    [_textFont release_mm];
    superdealloc_mm;
}

@end

@interface CUserGuideView()

@property (nonatomic, assign) CUserGuideBackgroundView *bgView;
@property (nonatomic, assign) id<CUserGuideViewDelegate> delegate;

@end

@implementation CUserGuideView

+ (CUserGuideView *)userGuideViewWithUnits:(NSArray *)arUnites delegate:(id<CUserGuideViewDelegate>)delegate
{
    CUserGuideView *guidView = [[[CUserGuideView alloc] initWithFrame:[UIScreen mainScreen].bounds guidUnits:arUnites] autorelease];
    guidView.delegate = delegate;
    return guidView;
}

- (void)show
{
    self.alpha = 0.0;
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [UIView animateWithDuration:.25 animations:^{
        self.alpha = 1.0;
    } completion:^(BOOL finished) {
    }];
}

- (void)dismiss
{
    [UIView animateWithDuration:.25 animations:^{
        self.alpha = 0.0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (id)initWithFrame:(CGRect)frame guidUnits:(NSArray *)units
{
    self = [super initWithFrame:frame];
    if (self)
    {
        // Initialization code
        self.bgView = [[[CUserGuideBackgroundView alloc] initWithFrame:self.bounds] autorelease];
        for (CUserGuideUnit *unit in units)
        {
            if (unit.type == EUserGuideUnitTypeCircleNode)
            {
                [self.bgView addTransparencyAreaWithFrame:unit.area];
            }
        }
        [self addSubview:self.bgView];
        
        for (CUserGuideUnit *unit in units)
        {
            if (unit.type == EUserGuideUnitTypeCircleNode)
            {
                CUserGuideCircleNoteView *view = [[CUserGuideCircleNoteView alloc] initWithFrame:unit.area];
                view.targetFrame = unit.area;
                view.information = unit.text;
                [self addSubview:view];
                [view release];
            }
            else if (unit.type == EUserGuideUnitTypeText)
            {
                CGSize textSize = [unit.text stringSizeWithFont:unit.textFont forConstrainSize:unit.area.size];
                UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(unit.area.origin.x+(unit.area.size.width-textSize.width)/2, unit.area.origin.y+(unit.area.size.height-textSize.height)/2, textSize.width, textSize.height)];
                lable.backgroundColor = [UIColor clearColor];
                lable.font = unit.textFont;
                lable.textColor = [UIColor whiteColor];
                lable.textAlignment = NSTextAlignmentCenter;
                lable.numberOfLines = 0;
                lable.text = unit.text;
                [self addSubview:lable];
                lable.alpha = 0.0;
                lable.frame = CGRectOffset(lable.frame, 0, -20);
                [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationCurveEaseOut animations:^{
                    lable.alpha = 1.0;
                    lable.frame = CGRectOffset(lable.frame, 0, 20);
                } completion:^(BOOL finished) {
                    
                }];
                [lable release];
            }
            else if (unit.type == EUserGuideUnitTypeImage)
            {
                UIImageView *imageView = [[UIImageView alloc] initWithImage:unit.image];
                imageView.center = CGPointMake(CGRectGetMidX(unit.area), CGRectGetMidY(unit.area)-20);
                imageView.alpha = 0.0;
                [self addSubview:imageView];
                [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationCurveEaseOut animations:^{
                    imageView.alpha = 1.0;
                    imageView.center = CGPointMake(CGRectGetMidX(unit.area), CGRectGetMidY(unit.area));
                } completion:^(BOOL finished) {
                    
                }];
                [imageView release];
            }
        }
        
        DispatchMainThreadSyncAfterDelay(1.5, ^
        {
            UITapGestureRecognizer *tapGesture = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureTrigger:)] autorelease];
            [self addGestureRecognizer:tapGesture];
        });
    }
    return self;
}

- (void)tapGestureTrigger:(UITapGestureRecognizer *)tapGesture
{
    if ([self.delegate respondsToSelector:@selector(userGuideViewHasClicked:)])
    {
        [self.delegate userGuideViewHasClicked:self];
    }
    else
    {
        [self dismiss];
    }
}

@end
